package com.gjy.mongo.sp;

import org.joda.time.LocalDateTime;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-01-30 10:41:09
 */
@Service
public class BookService {

    @Resource
    private BookRepository bookRepository;

    @PostConstruct
    public void init() {
//        run();
    }

    private void run() {
        String author = "yyy";
        String title = "qw12";
        String category = "zzz";

        Book build = Book.builder()
                .author(author)
                .title(title)
                .category(category)
                .publishDate(LocalDateTime.now())
                .build();
        bookRepository.save(build);

        build.setVoteCount(222);
        bookRepository.save(build);

        System.out.println("bookRepository.findOneByTitle(title) = " + bookRepository.findOneByTitle(title));

        System.out.println("bookRepository.findByAuthor(author) = " + bookRepository.findByAuthor(author));

        System.out.println("bookRepository.findByCategoryOrderByPublishDateDesc(category, PageRequest.of(0,10)) = " +
                bookRepository.findByCategoryOrderByPublishDateDesc(category, PageRequest.of(0, 10)));
    }
}
